import request from '../../utils/request'
const app = getApp();
Page({
  data: {
    shop_id: app.globalData.shop_id,
    address: [],
    cartList: [],
    order: {},
    totalPay: 0,
    ok: 1,
    loading: false,
    exec:false,
  },
  //页面刷新重新
  onShow(){
      this.getAddress()
  },
  async getCart(ids){
    let myData = ""
    ids.forEach(item =>{
        myData = myData +  item + ','
    })
    myData = myData.substr(0,myData.length - 1);
    let res = await request('/cart/getByIds',{ids:myData})
    let totalPay = 0
    res.data.forEach(item =>{
      totalPay += item.realPrice * item.goodsNum
    })
    totalPay = totalPay.toFixed(2);
    this.setData({
      totalPay,
      cartList: res.data
    })
  },
  onLoad() {
    let cartIds = wx.getStorageSync('cartIds')
    if(cartIds == null) return;
    this.getCart(cartIds)
  },
  // 创建订单
  postOrder(){
    wx.showToast({
      title: 'o(╥﹏╥)o',
      icon: 'none'
    })
    // const that = this
    // let order = {
    //   proInfoVoList: []
    // }
    // this.data.cartList.forEach(item =>{
    // })
    // wx.request({
    //   url: 'http://localhost:8000/order/add',
    //   method: 'POST',
    //   data: {order},
    //   success(res){
    //     if(res.data.code == 200){
    //       wx.showToast({
    //         title: '订餐成功',
    //         icon: 'success'
    //       })
    //     }
    //   },
    //   fail(){
    //     wx.showToast({
    //       title: '订餐失败',
    //       icon: 'error'
    //     })
    //   }
    // })
  },
  // 获取地理位置
  async getAddress(){
      let addressData = await request("/address/get");
      addressData.data.forEach(item => {
        if (item.isDefault) {
          this.setData({address:item})
        }
     })
  },
  navigateToAddress() {
    wx.navigateTo({
      url: '../addresses/addresses',
    });
  }
});
